package cn.com.gridinfo.sooc.core.course.pay.dao;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.utils.JsonUtil;
import com.jeremy.arad.utils.Log;
import com.loopj.android.http.RequestParams;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.com.gridinfo.sooc.SoocApplication;
import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.course.pay.bean.WeixinInfo;
import cn.com.gridinfo.sooc.core.course.pay.common.CodeConst;
import cn.com.gridinfo.sooc.utils.HttpUrl;

/**
 * 支付相关
 * Created by yingchunli on 2016/8/12.
 */
public class PayDao extends SoocIDao {
    private String status;//状态 0 成功 1 服务器错误  2 逻辑错误
    private String statusCode;//提示信息
    private String orderStatus;//订单支付状态：0：未支付，1：已支付
    private String buySuccessed;//0元课程是否购买成功
    private String weixinPackage;//微信支付所用
    private WeixinInfo weixinInfo;//微信支付所用
    private String param_string;//支付宝支付信息

    public PayDao(SoocNetResult iNetResult) {
        super(iNetResult);
    }

    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {
        if (result == null) return;
        status = result.get("status").asText();
        statusCode = result.get("statusCode").asText();
        Log.d("result->" + result.toString());
        switch (requestCode) {

            case CodeConst.PAY_ORDER:
                //生成订单
                SoocApplication.orderId = result.get("data").get("orderid").asText();
                buySuccessed = result.get("data").has("buySuccessed") ? result.get("data").get("buySuccessed").asText() : "";

            case CodeConst.PAY_RESULT:
                //支付结果
                orderStatus = result.get("data").has("status") ? result.get("data").get("status").asText() : "";
                break;

            case CodeConst.GET_WEIXIN_INFO:
                //获取微信支付信息
                weixinInfo = JsonUtil.node2pojo(result.get("data"), WeixinInfo.class);
                weixinPackage = result.get("data").has("package") ? result.get("data").get("package").asText() : "";
                break;

            case CodeConst.GET_ZFB_INFO:
                //获取支付宝信息
                param_string = result.get("data").get("param_string").asText();
//                body=%E4%BF%9D%E6%8A%A4%E7%8E%AF%E5%A2%83&notify_url=http%3A%2F%2Fdev.sooc.com%2Findex.php%2Fcourse%2FPublic%2Falipay_notify_url.html&out_trade_no=147183666107443586&return_url=http%3A%2F%2Fdev.sooc.com%2Findex.php%2Fcourse%2FIndex%2Fpay%2Forderid%2F147183666107443586.html&subject=%E4%BF%9D%E6%8A%A4%E7%8E%AF%E5%A2%83&total_fee=0.01&sign=&sign_type=
                break;
        }
    }

    /**
     * 生成订单
     *
     * @param kcid 课程id
     */
    public void payOrder(String kcid) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "order", true);
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        postRequest(url, params, CodeConst.PAY_ORDER);
    }

    /**
     * 订单结果
     *
     * @param orderId 订单号
     */
    public void orderResult(String orderId) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(), "getOrderPay", true);
        Map<String, String> map = new HashMap<>();
        map.put("orderid", orderId);
        getRequestForCode(url, map, CodeConst.PAY_RESULT);
    }

    /**
     * 获取微信支付信息
     *
     * @param orderId 订单号
     */
    public void getWeixinInfo(String orderId) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "weipay", true);
        Map<String, String> map = new HashMap<>();
        map.put("orderid", orderId);
        getRequestForCode(url, map, CodeConst.GET_WEIXIN_INFO);
    }

    /**
     * 支付宝支付信息
     *
     * @param orderId
     */
    public void getZfbInfo(String orderId) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "alipay", true);
        Map<String, String> map = new HashMap<>();
        map.put("orderid", orderId);
        getRequestForCode(url, map, CodeConst.GET_ZFB_INFO);
    }

    public String getStatus() {
        return status;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public String getOrderStatus() {
        return orderStatus;
    }

    public String getBuySuccessed() {
        return buySuccessed;
    }

    public String getWeixinPackage() {
        return weixinPackage;
    }

    public WeixinInfo getWeixinInfo() {
        return weixinInfo;
    }

    public String getParam_string() {
        return param_string;
    }
}
